<?php
class ControllerAccountEdit extends Controller {
	private $error = array();

	public function index() {
		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = HTTPS_SERVER . 'index.php?route=account/edit';

			$this->redirect(HTTPS_SERVER . 'index.php?route=account/login');
		}

		$this->language->load('account/edit');

		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('account/customer');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_account_customer->editCustomer($this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect(HTTPS_SERVER . 'index.php?route=account/account');
		}

      	$this->document->breadcrumbs = array();

      	$this->document->breadcrumbs[] = array(
        	'href'      => HTTP_SERVER . 'index.php?route=common/home',
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	); 

      	$this->document->breadcrumbs[] = array(
        	'href'      => HTTPS_SERVER . 'index.php?route=account/account',
        	'text'      => $this->language->get('text_account'),
        	'separator' => $this->language->get('text_separator')
      	);

      	$this->document->breadcrumbs[] = array(
        	'href'      => HTTPS_SERVER . 'index.php?route=account/edit',
        	'text'      => $this->language->get('text_edit'),
        	'separator' => $this->language->get('text_separator')
      	);
		
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_your_details'] = $this->language->get('text_your_details');
		$this->data['text_male'] = $this->language->get('text_male');
		$this->data['text_female'] = $this->language->get('text_female');
		
		$this->data['entry_thai_id'] = $this->language->get('entry_thai_id');
		$this->data['entry_user_id'] = $this->language->get('entry_user_id');
		$this->data['entry_gender'] = $this->language->get('entry_gender');
		$this->data['entry_ext'] = $this->language->get('entry_ext');
		$this->data['entry_firstname'] = $this->language->get('entry_firstname');
		$this->data['entry_lastname'] = $this->language->get('entry_lastname');
		$this->data['entry_email'] = $this->language->get('entry_email');
		$this->data['entry_telephone'] = $this->language->get('entry_telephone');
		$this->data['entry_fax'] = $this->language->get('entry_fax');
		$this->data['entry_birthday'] = $this->language->get('entry_birthday');
		$this->data['entry_birth_day'] = $this->language->get('entry_birth_day');
		$this->data['entry_birth_month'] = $this->language->get('entry_birth_month');
		$this->data['entry_birth_year'] = $this->language->get('entry_birth_year');
		$this->data['entry_facebook'] = $this->language->get('entry_facebook');
		$this->data['event'] = $this->language->get('event');
		$this->data['button_continue'] = $this->language->get('button_continue');
		$this->data['button_back'] = $this->language->get('button_back');

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->error['thai_id'])) {
			$this->data['error_thai_id'] = $this->error['thai_id'];
		} else {
			$this->data['error_thai_id'] = '';
		}
		
		if (isset($this->error['ext'])) {
			$this->data['error_ext'] = $this->error['ext'];
		} else {
			$this->data['error_ext'] = '';
		}
		
		if (isset($this->error['firstname'])) {
			$this->data['error_firstname'] = $this->error['firstname'];
		} else {
			$this->data['error_firstname'] = '';
		}

		if (isset($this->error['lastname'])) {
			$this->data['error_lastname'] = $this->error['lastname'];
		} else {
			$this->data['error_lastname'] = '';
		}
		
		if (isset($this->error['email'])) {
			$this->data['error_email'] = $this->error['email'];
		} else {
			$this->data['error_email'] = '';
		}	
		
		if (isset($this->error['telephone'])) {
			$this->data['error_telephone'] = $this->error['telephone'];
		} else {
			$this->data['error_telephone'] = '';
		}	

		$this->data['action'] = HTTPS_SERVER . 'index.php?route=account/edit';

		if ($this->request->server['REQUEST_METHOD'] != 'POST') {
			$customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
		}

		if (isset($this->request->post['thai_id'])) {

			$this->data['thai_id'] = $this->request->post['thai_id'];
		} elseif (isset($customer_info)) {
			$this->data['thai_id'] = $customer_info['thai_id'];
		} else {
			$this->data['thai_id'] = '';
		}
		
		if (isset($this->request->post['user_id'])) {

			$this->data['user_id'] = $this->request->post['user_id'];
		} elseif (isset($customer_info)) {
			$this->data['user_id'] = $customer_info['user_id'];
		} else {
			$this->data['user_id'] = '';
		}
		
		if (isset($this->request->post['gender'])) {

			$this->data['gender'] = $this->request->post['gender'];
		} elseif (isset($customer_info)) {
			$this->data['gender'] = $customer_info['gender'];
		} else {
			$this->data['gender'] = '';
		}
		
		if (isset($this->request->post['ext'])) {

			$this->data['ext'] = $this->request->post['ext'];
		} elseif (isset($customer_info)) {
			$this->data['ext'] = $customer_info['ext'];
		} else {
			$this->data['ext'] = '';
		}
			
		if (isset($this->request->post['firstname'])) {
			$this->data['firstname'] = $this->request->post['firstname'];
		} elseif (isset($customer_info)) {
			$this->data['firstname'] = $customer_info['firstname'];
		} else {
			$this->data['firstname'] = '';
		}

		if (isset($this->request->post['lastname'])) {
			$this->data['lastname'] = $this->request->post['lastname'];
		} elseif (isset($customer_info)) {
			$this->data['lastname'] = $customer_info['lastname'];
		} else {
			$this->data['lastname'] = '';
		}

		if (isset($this->request->post['email'])) {
			$this->data['email'] = $this->request->post['email'];
		} elseif (isset($customer_info)) {
			$this->data['email'] = $customer_info['email'];
		} else {
			$this->data['email'] = '';
		}

		if (isset($this->request->post['telephone'])) {
			$this->data['telephone'] = $this->request->post['telephone'];
		} elseif (isset($customer_info)) {
			$this->data['telephone'] = $customer_info['telephone'];
		} else {
			$this->data['telephone'] = '';
		}

		if (isset($this->request->post['fax'])) {
			$this->data['fax'] = $this->request->post['fax'];
		} elseif (isset($customer_info)) {
			$this->data['fax'] = $customer_info['fax'];
		} else {
			$this->data['fax'] = '';
		}
		
		if (isset($this->request->post['facebook'])) {
			$this->data['facebook'] = $this->request->post['facebook'];
		} elseif (isset($customer_info)) {
			$this->data['facebook'] = $customer_info['facebook'];
		} else {
			$this->data['facebook'] = '';
		}
		
		if (isset($this->request->post['birth_day'])) {
			$this->data['birth_day'] = $this->request->post['birth_day'];
		} elseif (isset($customer_info)) {
			$this->data['birth_day'] = $customer_info['birth_day'];
		} else {
			$this->data['birth_day'] = '';
		}
		
		if (isset($this->request->post['birth_month'])) {
			$this->data['birth_month'] = $this->request->post['birth_month'];
		} elseif (isset($customer_info)) {
			$this->data['birth_month'] = $customer_info['birth_month'];
		} else {
			$this->data['birth_month'] = '';
		}
		
		if (isset($this->request->post['birth_year'])) {
			$this->data['birth_year'] = $this->request->post['birth_year'];
		} elseif (isset($customer_info)) {
			$this->data['birth_year'] = $customer_info['birth_year'];
		} else {
			$this->data['birth_year'] = '';
		}

		$this->data['back'] = HTTPS_SERVER . 'index.php?route=account/account';
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/edit.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/account/edit.tpl';
		} else {
			$this->template = 'default/template/account/edit.tpl';
		}
		
		$this->children = array(
			'common/column_right',
			'common/footer',
			'common/column_left',
			'common/header'
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));		
	}

	private function validate() {
		
		//check thai id -- algorithm  --
		
		$thai_id = utf8_decode($this->request->post['thai_id']);
		
		$id_index =
		((substr($thai_id,0,1))*13)+
		((substr($thai_id,1,1))*12)+
		((substr($thai_id,2,1))*11)+
		((substr($thai_id,3,1))*10)+
		((substr($thai_id,4,1))*9)+
		((substr($thai_id,5,1))*8)+
		((substr($thai_id,6,1))*7)+
		((substr($thai_id,7,1))*6)+
		((substr($thai_id,8,1))*5)+
		((substr($thai_id,9,1))*4)+
		((substr($thai_id,10,1))*3)+
		((substr($thai_id,11,1))*2);
	
		$id_index = ($id_index % 11);
		$id_index = (11 - $id_index);
		
		if(strlen($id_index) == 2){$id_index = substr($id_index,1,1);}
		
		
		if ((strlen($thai_id) !=13) || substr($thai_id,12,1) != $id_index) {
      		$this->error['thai_id'] = $this->language->get('error_thai_id');
		}
		
		if ((strlen(utf8_decode($this->request->post['firstname'])) < 1) || (strlen(utf8_decode($this->request->post['firstname'])) > 32)) {
			$this->error['firstname'] = $this->language->get('error_firstname');
		}

		if ((strlen(utf8_decode($this->request->post['lastname'])) < 1) || (strlen(utf8_decode($this->request->post['lastname'])) > 32)) {
			$this->error['lastname'] = $this->language->get('error_lastname');
		}

		if ((strlen(utf8_decode($this->request->post['email'])) > 96) || (!preg_match(EMAIL_PATTERN, $this->request->post['email']))) {
			$this->error['email'] = $this->language->get('error_email');
		}
		
		if (($this->customer->getEmail() != $this->request->post['email']) && $this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
			$this->error['warning'] = $this->language->get('error_exists');
		}

		if ((strlen(utf8_decode($this->request->post['telephone'])) < 3) || (strlen(utf8_decode($this->request->post['telephone'])) > 32)) {
			$this->error['telephone'] = $this->language->get('error_telephone');
		}

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
}
?>